package com.port.serial;

import java.util.Date;

import android.os.Message;
import android.util.Log;

import com.port.serial.MainActivity.EventHandler;


public class SerialComm extends Thread {

	static { System.loadLibrary("serialtest"); }

	private native int openserial(String devPath, String strBaud, int vtime, int vmin);
	private native int closeserial(int fd);
	private native int[] readserial(int fd);
	/*
	private native String readserial(int fd, int length);
	 */

	int COMM_RECV=102;

	Date m_time_set;
	EventHandler m_eventHandler;
	int m_ilengthBuf;
	int m_fd;
	int n;
	String m_strReadBuf;
	boolean m_read_complete;
	
	SensingData badAir;
	


	public SerialComm(EventHandler eventHandler, int lengthBuf) {
		m_eventHandler = eventHandler;
		m_ilengthBuf = lengthBuf;
		m_time_set = new Date();
		m_fd = 0;
		m_strReadBuf = new String("");
		m_read_complete = true;
		
		badAir = new SensingData();
		AppManager.getInstance().sensingData = badAir;
		
		
		

		return;
	}

	public int openPort(String devPath, int nBaudRate, int vtime, int vmin) {
		if (m_fd != 0) return 0;

		m_fd = openserial(devPath, String.valueOf(nBaudRate), vtime, vmin);

		return m_fd;
	}

	public int closePort() {
		if (m_fd == 0) return 0;

		closeserial(m_fd);
		m_fd = 0;

		return 1;
	}

	public void run() {
		if (m_fd == 0) return;

		try {
			while (true) {
				if ( (m_fd > 0) && m_read_complete) {
					int data;
					int id;
					


					int[] read_bytes = readserial(m_fd);
					if(read_bytes.length > 0) {
						
						//if (read_bytes.length > 0) {
						//m_strReadBuf = read_bytes.toString();

						for (int i = 0; i < read_bytes.length; i++) {

							data = (int)read_bytes[i];

							m_strReadBuf += Integer.toHexString(data);
							
							if (i == 15) {	// id 
								m_strReadBuf += "      ";

								id = read_bytes[15];

								if (id == 1) {	// NO
									badAir.setNo(16 *read_bytes[22] + read_bytes[23]);
								} else if (id == 2) { // CO2
									badAir.setCo2(16 *read_bytes[22] + read_bytes[23]);
								} else if (id == 3) { // O3
									badAir.setO3(16 *read_bytes[22] + read_bytes[23]);
								} else if (id == 4) { // DUST
									badAir.setDust(16 *read_bytes[22] + read_bytes[23]);
								} else if (id == 5) { // VOC
									badAir.setVoc(16 *read_bytes[22] + read_bytes[23]);
								} else if (id == 6) { // CO
									badAir.setCo(16 *read_bytes[22] + read_bytes[23]);
								}
							}

							m_strReadBuf += " ";
						}
						Log.i("value", Integer.toString(badAir.getNo()) + " " +
								Integer.toString(badAir.getCo2()) + " " +
								Integer.toString(badAir.getO3()) + " " +
								Integer.toString(badAir.getDust()) + " " +
								Integer.toString(badAir.getVoc()) + " " +
								Integer.toString(badAir.getCo()));
//						Log.i("count", m_strReadBuf);
						m_read_complete = false;
						Message msg = m_eventHandler.obtainMessage();
						msg.what = COMM_RECV;
						m_eventHandler.sendMessage(msg);
					}

				}

				Thread.sleep(500);
			}
		} catch (Exception e) {}
	}


	public String recv() {
		String tmp;

		tmp = new String(m_strReadBuf);
		m_strReadBuf = "";
		m_read_complete = true;
		return tmp;
	}

}
